//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.controller
{
	import flash.desktop.NativeApplication;
	import flash.desktop.SystemTrayIcon;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.NativeMenu;
	import flash.display.NativeMenuItem;
	import flash.events.Event;
	import flash.events.ScreenMouseEvent;
	import flash.net.URLRequest;
	
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.command.SimpleCommand;
	
	import reader.ApplicationFacade;
	import reader.core.ReaderModelLocator;
	import reader.core.ReaderResource;
	import reader.core.WindowManager;
	import reader.view.components.TypedWindow;

	public class TrayCommand extends SimpleCommand implements ICommand
	{
		//import flash.display.BitmapData;
		private var dockImage:BitmapData;
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
	
		override public function execute(notification:INotification):void
		{
			if(NativeApplication.supportsSystemTrayIcon)
			{
				if(!dockImage)
				{
					var image:Loader = new Loader();				
					image.contentLoaderInfo.addEventListener(Event.COMPLETE, parpareTray_handler);
					image.load(new URLRequest("ico/reader_16.png"));
				}
			}
			else
			{
				//TODO: Alert
			}
			
		}
		
		private function parpareTray_handler(e:Event):void
		{
			dockImage = e.target.content.bitmapData as BitmapData;
			trayDock();
		}
		
		private function trayDock():void
		{
			NativeApplication.nativeApplication.icon.bitmaps = [dockImage];
			var trayIcon:SystemTrayIcon = NativeApplication.nativeApplication.icon as SystemTrayIcon;
			trayIcon.tooltip = ReaderResource.instance.resource.ganeralAppName;
			trayIcon.menu = createSystrayRootMenu();
			trayIcon.addEventListener(ScreenMouseEvent.CLICK, trayUndock);
			WindowManager.eachWindow(closeWindow);
			ReaderModelLocator.instance.airReader.nativeWindow.minimize();
			ReaderModelLocator.instance.airReader.nativeWindow.visible = false;
			sendNotification(ApplicationFacade.SYS_DOCKED);
		}
		
		public function createSystrayRootMenu():NativeMenu
		{   
            var menu:NativeMenu = new NativeMenu();   
            var openNativeMenuItem:NativeMenuItem = new NativeMenuItem(ReaderResource.instance.resource.appUnDuck);
            var exitNativeMenuItem:NativeMenuItem = new NativeMenuItem(ReaderResource.instance.resource.appQuit);
            openNativeMenuItem.addEventListener(Event.SELECT, trayUndock);   
            exitNativeMenuItem.addEventListener(Event.SELECT, closeApp);
            menu.addItem(openNativeMenuItem);
            menu.addItem(exitNativeMenuItem);
            return menu;   
  
        }   
		
		private function trayUndock(e:Event = null):void
		{
			ReaderModelLocator.instance.airReader.nativeWindow.visible = true;
			ReaderModelLocator.instance.airReader.nativeWindow.restore();
			WindowManager.eachWindow(showWindow);
			ReaderModelLocator.instance.airReader.nativeWindow.orderToFront();
			sendNotification(ApplicationFacade.SYS_COMMON);
		}
		
		private function closeWindow(w:TypedWindow):void
		{
			w.minimize()
			w.nativeWindow.visible = false;
		}
		
		private function showWindow(w:TypedWindow):void
		{
			w.nativeWindow.visible = true;
			w.restore();
		}
		
		private function closeApp(e:Event):void
		{
			sendNotification(ApplicationFacade.CLOSE_WINDOW);
		}
	}
}